package com.cbts.action.info;


import java.util.ArrayList;
import java.util.List;



import com.cbts.entity.Department;
import com.cbts.service.info.DepartmentService;
import com.cbts.service.info.UserInfoService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * @author 邱鸿辉
 *泰坦
 */
public class DepartmentAction extends ActionSupport{


	private static final long serialVersionUID = 1L;
	Department department;								//部门
	List<Department> departmentList;					//部门列表
	private String check;								//被用户勾选的部门的departmentId构成的字符串
	private String operate;								//用户进行的操作：修改或者修改部门信息
	private DepartmentService departmentService;		//提供部门管理相关的服务
	private UserInfoService userInfoService;
	private String message;
	

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getCheck() {
		return check;
	}
	public void setCheck(String check) {
		this.check = check;
	}
	public String getOperate() {
		return operate;
	}
	public void setOperate(String operate) {
		this.operate = operate;
	}
	public DepartmentService getDepartmentService() {
		return departmentService;
	}
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	public List<Department> getDepartmentList() {
		return departmentList;
	}
	public void setDepartmentList(List<Department> departmentList) {
		this.departmentList = departmentList;
	}
	public String addDepartment(){
		try{
			Department dep = departmentService.getDepartment(department.getDepartmentId());
			if(dep != null)
			{
				message = "部门号已存在，增加失败";
				return ERROR;
			}
			dep = departmentService.getDepartmentByDepartmentName(department.getDepartmentName());
			if(dep != null)
			{
				message = "部门名已存在，增加失败";
				return ERROR;
			}
			if(departmentService.addDepartment(department))
				message = "部门添加成功";
			else
				message = "部门添加失败";
			return SUCCESS;		
		}catch(Exception e){
			e.printStackTrace();
			//message ="部门添加失败";
			return INPUT;
		}
		
		
	}
	public String updateDepartment(){
		try{
		if(departmentService.sameDepartmentNameExist(department.getDepartmentId(), department.getDepartmentName()))
		{
			message ="部门名已存在，修改失败";
			return ERROR;
		}
		if(departmentService.updateDepartment(department))
			message = "部门修改成功";
		else
		{
			message = "部门修改失败";
			return ERROR;
		}
		return SUCCESS;	
		}catch(Exception e){
			e.printStackTrace();
			return INPUT;
		}
		
	}
	public String deleteDepartment(){
			try{
				/*if(userInfoService.getUserInfoByDepartment(department.getDepartmentId())!= null)
				{
					message = "该部门有所属员工，请先修改员工所属部门，再删除部门";
				}*/
				if(departmentService.deleteDepartment(department))
					message = "部门删除成功";
				else
				{
					message = "部门删除失败";
					return ERROR;
				}
				return SUCCESS;		
			}catch(Exception e){
				e.printStackTrace();
				return INPUT;
			}
		
	}


	//获取所有的部门，用于页面显示
	public String showDepartmentList(){
		try{
			if(message != null)
			{
				ActionContext.getContext().put("message", message);
				message = null;
			}
			departmentList = departmentService.getAllDepartment();
			return SUCCESS;	
		}catch(Exception e){
			e.printStackTrace();
			return INPUT;
		}
	
	
	}
	//修改或者删除用户选中的部门
	public String updateOrDeleteDepartment(){
		try{
			
			Department dep = null;
			List<Department> selectedList = null;
			List<Department> leftList = new ArrayList<Department>();
			if( departmentList != null)	
				 selectedList = departmentService.getSelectedDepartmentList(departmentList,check,operate);
			if(selectedList != null)
			{
				for(int i=0;i<selectedList.size();i++){
					dep = selectedList.get(i);
					for(int j=i+1;j<selectedList.size();j++)
						if(dep.getDepartmentName().equals(selectedList.get(j).getDepartmentName()))
						{
							message = "部门名不可相同";
							return ERROR;
						}
				}
				
				if ("删除".equals(operate)) {
						 if(departmentService.deleteDepartmentList(selectedList))// 批量删除用户信息
							 message="删除成功";
						 else 
						 {
							 message ="删除失败";
							 return ERROR;
						 }
					
				} else if ("修改".equals(operate)) {
						departmentService.updateDepartmentList(selectedList);// 批量更新用户信息
							message="修改成功";
				
				} 
			}
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			if("删除".equals(operate))
				message="删除失败";
			if("修改".equals(operate))
				message="修改失败";
			return INPUT;
		}
	}
}
